package com.springboot.wisdom.common.common.constant;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

import java.util.Objects;

/**
 * @Description
 * @Author LY L
 * @Date 2024-11-09
 */
@ToString
@Getter
@AllArgsConstructor
public enum LogStatusEnum {

    SUCCESS(200, "SUCCESS", "正常"),
    PARAM_ERROR(10001, "PARAM_ERROR", "参数不正确"),
    USER_ACCOUNT_ERROR(20001, "USER_ACCOUNT_ERROR", "用户账号密码错误"),
    USER_LOGIN_ERROR(20002, "USER_LOGIN_ERROR", "用户未登录"),
    BUSINESS_ERROR(30003, "BUSINESS_ERROR", "业务请求失败"),
    AUTH_ERROR(40001, "AUTH_ERROR", "认证失败或Token过期"),

    // 账号相关。
    REGISTER(0, "REGISTER", "注册成功"),
    REGISTER_ERROR(1, "REGISTER_ERROR", "注册失败"),
    LOGIN(2, "LOGIN", "登录成功"),
    LOGOUT(3, "LOGOUT", "退出登录"),
    LOGIN_ERROR(4, "LOGIN_ERROR", "登录失败"),
    ACCOUNT_DISABLE(5, "ACCOUNT_DISABLE", "账号已被禁用"),
    // 个人中心。
    UPDATE_INFO_NAME(10, "UPDATE_INFO_NAME", "修改名称"),
    UPDATE_INFO_PHONE(11, "UPDATE_INFO_PHONE", "修改手机号"),
    UPDATE_INFO_PASSWORD(12, "UPDATE_INFO_PASSWORD", "修改密码"),
    UPDATE_INFO_AVATAR_URL(13, "UPDATE_INFO_AVATAR_URL", "修改头像"),

    // 邮箱。
    SEND_EMAIL(20, "SEND_EMAIL", "发送邮件"),
    // 联系人管理
    CONTACT_ADD(30, "CONTACT_ADD", "添加联系人"),
    CONTACT_UPDATE(31, "CONTACT_UPDATE", "修改联系人"),
    CONTACT_DELETE(32, "CONTACT_DELETE", "删除联系人"),
    CONTACT_DELETE_BATCH(33, "CONTACT_DELETE_BATCH", "删除联系人【批量】"),


    // 管理员操作。
    RE_PASSWORD(100, "RE_PASSWORD", "重置密码"),
    ACCOUNT_STATUS(101, "ACCOUNT_STATUS", "账号状态"),
    ACCOUNT_DELETE(102, "ACCOUNT_DELETE", "注销账号"),
    ;
    private final int index;
    @EnumValue // 标记数据库存的值是code
    @JsonValue //标记json返回的值
    private final String value;
    // 描述
    private final String details;


    /**
     * 根据code查找
     *
     * @param value 枚举值。
     * @return 枚举对象
     */
    public static LogStatusEnum findEnumByValue(String value) {
        for (LogStatusEnum logEnum : LogStatusEnum.values()) {
            if (logEnum.getValue().equalsIgnoreCase(value)) {
                //如果需要直接返回name则更改返回类型为String,return logEnum.name;
                return logEnum;
            }
        }
        return null;
    }

    /**
     * 根据name查找
     *
     * @param index 枚举index。
     * @return 枚举对象
     */
    public static LogStatusEnum findEnumByIndex(int index) {
        for (LogStatusEnum logEnum : LogStatusEnum.values()) {
            if (logEnum.getIndex() == index) {
                //如果需要直接返回code则更改返回类型为String,return logEnum.code;
                return logEnum;
            }
        }
        return null;
    }

    public static String findEnumByIndexToString(String value) {
        return Objects.requireNonNull(findEnumByValue(value)).getValue();
    }
}
